//
//  AppDelegate.m
//  17-SandBox
//
//  Created by GivenBmli on 16/8/5.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "AppDelegate.h"
#import "Book.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
//    SandBox(沙盒)
//   Documents(文件文档, 用户数据存储)
//    Library(资源,一般用来存放,程序员要存储的一些数据)
//    ↓
//    Caches(缓存文件)
//    Prerences (用户信息, 用户设置,NSUserDefaults)
//    tmp(临时目录,下载的临时文件一般放这里)
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults]synchronize];
//    获取路径
//    快捷获取到对应的C函数
//    根目录,家目录
    NSLog(@"Homw--------%@",NSHomeDirectory());
//    临时目录tmp
    NSLog(@"Temporary------%@",NSTemporaryDirectory());
//    C函数
//    参数1:搜索哪个文件路径
//    常用:NSDocumentDirectory , NSLibraryDirectory , NSCachesDirectory
//    参数2:在 用户 作用域下搜索
//    参数3:Yes or NO ,yes代表局对路径(基本上都用绝对的 NO代表相对路径(~))
   NSArray *pathArray  = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"pathArray----%@", [pathArray firstObject]);
//    NSBundle .app文件包
    NSLog(@"%@", [NSBundle mainBundle]);
//    1.简单的文件读写 Input Output
    NSString *helloSting = @"Hello ,I/O File";
//    一般拼接路径时,使用stringByAppendingPathComponent
//    NSString *weitePath = [[pathArray firstObject] stringByAppendingString:@"/hello.txt"];
    NSString *weitePath = [[pathArray firstObject]stringByAppendingPathComponent:@"hello.txt"];
    NSError *error = nil;
   BOOL isSuccess = [helloSting writeToFile:weitePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"存储失败");
    }else {
        NSLog(@"存储成功");
    }
    
//   2/读取路劲对应得文字
    NSError *readeError = nil;
   NSString *readSting =  [NSString stringWithContentsOfFile:weitePath encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%@", readSting);
    
//    3/将数组 写入本地文件
    NSArray *array = @[@"韩旭",@"王煌",@"包铭礼",@"郭宝"];
    NSString *arrayPath = [[pathArray firstObject]stringByAppendingString:@"name.plist"];
   BOOL isArrayWriteSuccess = [array writeToFile:arrayPath atomically:YES];
    if (isArrayWriteSuccess) {
        NSLog(@"写入成功");
    }else{
        NSLog(@"写入失败");
    }
//    将数组 读入
   NSArray *nameArray = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"%@", nameArray);
//    将字典写入本地
    NSDictionary *dic = @{
                          @"name":@"bao",
                          @"age" :@"13",
                          @"sex" :@"nag"
                          };
//    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dic];
    NSString *dicPath = [[pathArray firstObject]stringByAppendingString:@"dic.plist"];
   BOOL isDicSuccess =  [dic writeToFile:dicPath atomically:YES];
    if (isDicSuccess) {
        NSLog(@"写入成功");
    }else{
        NSLog(@"写入失败");
    }

//    6 将字典 读入
   NSDictionary *dicConter = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"%@",dicConter);
    
//   7 将Data类型写入本地
    UIImage *imag = [UIImage imageNamed:@"1"];
//    将UIimag图转二进制
NSData *imageData = UIImageJPEGRepresentation(imag, 0.1);
    NSLog(@"imaged = %@",imageData);
    UIImagePNGRepresentation(imag);
    NSString *dataPata = [[pathArray firstObject]stringByAppendingString:@"imageData.a"];
  BOOL isdataWriteSuccess = [imageData writeToFile:dataPata atomically:YES];
    if (isdataWriteSuccess) {
        NSLog(@"写入成功");
    }else{
        NSLog(@"写入失败");
    }
//    8NSData 类型 读入
    NSData *imageNewData = [NSData dataWithContentsOfFile:dataPata];
  UIImage *fileIamge = [UIImage imageWithData:imageNewData];
//    2.复杂对象文件读写,自定义类型
//    归档/反归档 序列化/反序列化
//    归档,将对象存储到本地
    Book *book = [Book new];
    book.bookName = @"iOS从入门到放弃";
    book.bookType = @"教育";
    book.bookPrice = @"999.99";
    book.bookAuthor = @"王煌";
    book.bookAddress = @"王煌黑科技出版社";
    NSString *bookPath = [[pathArray firstObject]stringByAppendingPathComponent:@"Book.plist"];
 BOOL isBookWriteSuccess = [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    if (isBookWriteSuccess) {
        NSLog(@"写入成功");
    }else{
        NSLog(@"写入失败");
    }
    
    
//   2.反归档
    
  Book *huangBook = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
    NSLog(@"Book = %@", huangBook.bookName);
//    如果对象想要实现归档和反归档
//    1对象对应的类需要签订<NSCoding>
//    2实现协议方法
//      1>initWithCoder 反归档用
//      2>encodeWithCoder归档用
//    3.归档时,使用NSkeyedArchiver
//    4.反归档时,使用 NSkeyUnarchiver
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
